﻿namespace libsemtd
{
    partial class Semtd
    {
        private void labelReset()
        {
            this.labelVer = new int[this.numVerMain];
            this.labelEdges = new int[this.numVerMain, this.numVerMain];
            this.labelEdgeAbsoluteMax = 2 * this.numVerTotal - 1;
            this.labelEdgesUsed = new int[this.labelEdgeAbsoluteMax + 1];
            //this.scoreCache = -1;
        }
        private int labelRangeEdge()
        {
            labelRefreshEdgeMinMax();
            return this.labelEdgeMax - this.labelEdgeMin + 1;
        }
        private void labelRefreshEdgeMinMax()
        {
            for (int i = 1; i <= this.labelEdgeAbsoluteMax; i++)
            {
                if (this.labelEdgesUsed[i] > 0)
                {
                    this.labelEdgeMin = i;
                    break;
                }
            }
            for (int i = this.labelEdgeAbsoluteMax; i >= 1; i--)
            {
                if (this.labelEdgesUsed[i] > 0)
                {
                    this.labelEdgeMax = i;
                    break;
                }
            }
        }
        private void labelRemoveVer(int posVer)
        {
            if (this.labelVer[posVer] > 0)
            {
                this.labelVer[posVer] = 0;
                labelRemoveEdges(posVer);
            }
        }
        private void labelRemoveEdges(int posVer)
        {
            for (int i = 0; i < this.numVerMain; i++)
            {
                if (this.labelVer[i] > 0 && i != posVer && this.graphConn[posVer, i])
                {
                    int labelOld = this.labelEdges[i, posVer];
                    this.labelEdgesUsed[labelOld]--;
                    this.labelEdges[i, posVer] = this.labelEdges[posVer, i] = 0;
                }
            }
        }
        private void labelSetVer(int posVer, int labelVer)
        {
            if (this.labelVer[posVer] > 0) { labelRemoveVer(posVer); }
            this.labelVer[posVer] = labelVer;
            labelSetEdges(posVer);
        }
        private void labelSetEdges(int posVer)
        {
            int labelNew;
            for (int i = 0; i < this.numVerMain; i++)
                if (i != posVer &&
                    this.graphConn[posVer, i] &&
                    this.labelVer[i] > 0)
                {
                    labelNew = this.labelVer[posVer] + this.labelVer[i];
                    this.labelEdges[i, posVer] = this.labelEdges[posVer, i] = labelNew;
                    this.labelEdgesUsed[labelNew]++;
                }
        }
        private bool isValidToLabel(int posVer, int labelVer)
        {
            for (int i = 0; i < this.numVerMain; i++)
            {
                if (this.graphConn[i, posVer] &&
                    this.labelVer[i] > 0 &&
                    this.labelEdgesUsed[this.labelVer[i] + labelVer] == 1)
                {
                    return false;
                }
            }
            return true;
        }
        private bool isSemt()
        {
            if (labelRangeEdge() == numEdges)
            {
                for (int i = this.labelEdgeMin; i <= this.labelEdgeMax; i++)
                {
                    if (this.labelEdgesUsed[i] != 1)
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
        private int minDef()
        {
            int ret = 0;
            switch (this.graphType)
            {
                case "wheel": //source: dissertation[1]
                    switch (this.numVerMain)
                    {
                        case 3 | 4 | 6 | 7:
                            ret = 1;
                            break;
                        default:
                            ret = 2;
                            break;
                    }
                    break;
                case "fan": //source: dissertation[1]
                    if (this.numVerMain <= 7)
                        ret = 0;
                    else
                        ret = 1;
                    break;
                case "doublefan": //source: dissertation[1]
                    ret = (this.numVerMain - 1) / 2;
                    break;
                default:
                    // ret = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(this.numEdges / 2))) + 2 - this.numVertices;
                    //break;
                    ret = -1;
                    break;
            }
            return ret;
        }
    }
}